import os
import sys
import win32api

from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class StartCloseDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.init_ui()

    def init_ui(self):
        grid = QGridLayout()
        self.setLayout(grid)
        for line in range(10):
            qlb11 = QLabel('APP' + str(line + 1), self)
            qbtn1 = QPushButton('Start', self)
            qbtn1.clicked.connect(self.start)
            qbtn2 = QPushButton('Stop', self)
            qbtn2.clicked.connect(self.stop)
            grid.addWidget(qlb11, *(line, 0))
            grid.addWidget(qbtn1, *(line, 1))
            grid.addWidget(qbtn2, *(line, 2))
        self.resize(250, 150)
        self.center()
        self.setWindowTitle('Quick Start')
        self.setWindowIcon(QIcon("./ext/StartClose/Dance.jpg"))
        self.show()

    def center(self):
        fg = self.frameGeometry()
        qdwc = QDesktopWidget().availableGeometry().center()
        fg.moveCenter(qdwc)
        self.move(fg.topLeft())

    @staticmethod
    def start():
        win32api.ShellExecute(0, 'open', 'C:/Users/lz/ngrok.exe', '', '', 1)
        # subprocess.Popen("C:/Users/lz/ngrok.exe")

    @staticmethod
    def stop():
        os.system("taskkill /F /IM ngrok.exe")


def _test_start_close():
    """
    _test_start_close()
    """
    app = QApplication(sys.argv)
    scd = StartCloseDemo()
    sys.exit(app.exec_())


if __name__ == '__main__':
    _test_start_close()
